import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/models/base_advanced_filter_model.dart';
import 'package:x3_form_core/filter_components/mixin/advanced_filter_select_mixin.dart';

/// author：william on 2021/4/9 14:00
/// email：362661044@qq.com

class AdvancedFilterDateRange extends StatefulWidget {
  final BaseAdvancedFilterModel model;

  const AdvancedFilterDateRange({Key key, @required this.model}) : super(key: key);

  @override
  _AdvancedFilterDateRangeState createState() => _AdvancedFilterDateRangeState();
}

class _AdvancedFilterDateRangeState extends State<AdvancedFilterDateRange> with AdvancedFilterSelectMixin {
  final String customValue = '自定义';
  final String customKey = 'custom';

  Map<String, dynamic> toDay;
  Map<String, dynamic> toMonth;
  Map<String, dynamic> within30Days;
  Map<String, dynamic> within3Months;

  @override
  List<BaseKeyValueModel> get children => [
        BaseKeyValueModel(key: allKey, value: '全部'),
        BaseKeyValueModel(key: toDay, value: '本日'),
        BaseKeyValueModel(key: toMonth, value: '本月'),
        BaseKeyValueModel(key: within30Days, value: '最近30天'),
        BaseKeyValueModel(key: within3Months, value: '最近3个月'),
        BaseKeyValueModel(key: customKey, value: customValue),
      ];

  @override
  BaseAdvancedFilterModel get model => widget.model;

  @override
  void initStateEvent() {
    DateTime now = DateTime.now();
    DateTime _toDayDateTime = DateTime(now.year, now.month, now.day);
    DateTime _toMonthDateTime = DateTime(now.year, now.month);
    DateTime _within30DaysDateTime = _toDayDateTime.add(Duration(days: -30));
    DateTime _within3MonthsDateTime = DateTime(_toDayDateTime.year, _toDayDateTime.month - 3, _toDayDateTime.day);
    toDay = {"beginDate": BaseTransformUtil.dateTimeAsString(_toDayDateTime), "endDate": BaseTransformUtil.dateTimeAsString(_toDayDateTime)};
    toMonth = {"beginDate": BaseTransformUtil.dateTimeAsString(_toMonthDateTime), "endDate": BaseTransformUtil.dateTimeAsString(_toDayDateTime)};
    within30Days = {"beginDate": BaseTransformUtil.dateTimeAsString(_within30DaysDateTime), "endDate": BaseTransformUtil.dateTimeAsString(_toDayDateTime)};
    within3Months = {"beginDate": BaseTransformUtil.dateTimeAsString(_within3MonthsDateTime), "endDate": BaseTransformUtil.dateTimeAsString(_toDayDateTime)};

    /// 检查是存在数据
    var _data = baseRequestFormProvider.getDataKey(model);
    if (_data != null) {
      /// 如果有数据 而且 选择项为0， 将勾选自定义
      if (_data is List) {
        /// 检查是否有默认值的范围内
        List<String> list = children.map((e) => e.key.toString()).toList();
        if (!list.contains(_data.toString())) {
          selectedList.clear();
          selectedList.add(BaseKeyValueModel(key: customKey, value: customValue));
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return buildItemContainer();
  }

  @override
  void onChange(bool v, BaseKeyValueModel data) {
    if (data.value != customValue) {
      onSingleChangeEvent(v, data);
      baseRequestFormProvider.setDataSource(this.model, selectedList.length == 1 ? selectedList.first.toJson() : {});
      setState(() {});
    } else {
      /// 自定义按钮
      dynamic value = baseRequestFormProvider.getDataKey(model);
      DateTimeRange _initialDateRange;
      if (value is Map) {
        DateTime beginDate = BaseTransformUtil.stringAsDateTime(date: value['beginDate']).toLocal();
        DateTime endDate = BaseTransformUtil.stringAsDateTime(date: value['endDate']).toLocal();
        _initialDateRange = DateTimeRange(start: beginDate, end: endDate);
      }
      BaseComponentUtils.openDateRangePicker(context, initialDateRange: _initialDateRange).then((value) {
        if (value != null) {
          selectedList.clear();
          selectedList.add(data);
          baseRequestFormProvider.setDataSource(this.model, {
            'key': {"beginDate": BaseTransformUtil.dateTimeAsString(value.start), "endDate": BaseTransformUtil.dateTimeAsString(value.end)},
            'value': customValue
          });
          setState(() {});
        }
      });
    }
  }
}
